package com.stx.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.entity.TMedicine;
import com.stx.jdbc.JdbcUtil;

/**
 * Servlet implementation class showdmservlet
 */
public class showdmservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public showdmservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String did =request.getParameter("did");
		//查看该科室的所有药品
		Connection con=null;
		PreparedStatement pst=null;
		List<TMedicine> medlist=new ArrayList<TMedicine>();
		 try {
			con=JdbcUtil.getCon();
			 pst=con.prepareStatement("select  mid ,mname,m.mtypeid,mformid,mfactoryid,prescription,expiration, mcount,mfunction from tbl_prescription p,tbl_department d,tbl_medicine m where p.did=d.did and p.mtypeid=m.mtypeid");
//				pst.setString(1, did);
				ResultSet rs =pst.executeQuery();
				while(rs.next()){
					TMedicine med=new TMedicine();
					med.setMcount(rs.getString("mcount"));
					med.setMfactoryid(rs.getString("mfactoryid"));
					med.setMformid(rs.getString("mformid"));
					med.setMfunction(rs.getString("mfunction"));
					med.setMid(rs.getString("mid"));
					med.setMname(rs.getString("mname"));
					med.setMtypeid(rs.getString("mtypeid"));
					med.setExpiration(rs.getString("expiration"));
					med.setPrescription(rs.getString("prescription"));
					medlist.add(med);
			}} catch (ClassNotFoundException | SQLException e) {
				e.printStackTrace();
			}finally {
				JdbcUtil.closeCon(con, pst);
			}
		 request.getSession().setAttribute("did", did);
		 request.setAttribute("medlist",medlist);
		 request.getRequestDispatcher("/admin/showdm.jsp").forward(request, response);
	}

}
